package com.easou.ps.dk.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

/**
 * 保存网页
 * @author dirk
 * 2012-6-18 上午08:11:43
 */
public class HtmlUtil {
	public static final Logger LOG_CRAWLING = Logger.getLogger("crawling");
	
	/**
	 * 返回头三条结果的url
	 * @param ketword
	 * @param num
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<String> saveHtml(String keyword, int num){
		keyword = replace(keyword);
		WebClient webClient = new WebClient();
//		webClient.setTimeout(40000);
		List<String> urls = new ArrayList<String>();
		String url = "";
		try {
			url = "http://"+Configer.get("host")+"/s.m?q="+URLEncoder.encode(keyword,"UTF-8")+"&sty=1&c=6&fr=14.0.10.111&cid=&chn_i=%E6%90%9C%E7%B4%A2&esid=LJmaHUBlUQd&wver=t&w=1&debug=1&clearCache=1";
			LOG_CRAWLING.info("URL:"+url);
			HtmlPage page = webClient.getPage(url);
			List<HtmlElement> elements = (List<HtmlElement>) page.getByXPath("//div[@class=\"iterm\"]/a[1]");
			int max = Integer.valueOf(Configer.get("compare"));
			for (int i = 0; i < (elements.size()>max?max:elements.size()); i++) {
				String href = elements.get(i).getAttribute("href").replaceAll("%26", "&").replaceAll("&amp;", "&");//去除URL编码
				String target = RegexHelper.getValueByPattern(href, "sr=(.*?)&");
				urls.add(i+"-"+keyword+":"+URLDecoder.decode(target, "UTF-8"));
				LOG_CRAWLING.info(keyword+"_"+i+":"+target);
			}
			String fileName = Configer.get("data_path")+GetDay.getToday()+"/"+num+"_"+keyword+".html";
			File file = new File(fileName);
			System.out.println("save : "+fileName);
			FileOutputStream fos = new FileOutputStream(file);
			byte[] bytes = page.asXml().getBytes();
			fos.write(bytes);
			fos.close();
//			page.save(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
//		for (int i = 0; i < urls.size(); i++) {
//			System.out.println(urls.get(i));
//		}
		return urls;
	}
	
	public static String replace(String src){
		String result = src;
		result = result.replaceAll(":", "：");
		result = result.replaceAll("\\?", "？");
		result = result.replaceAll("\\<", "《");
		result = result.replaceAll("\\>", "》");
		return result;
	}
	
}
